<?php
set_time_limit(0);
include_once("./common.php");

/***********调用*************/
// $microShopObj = new InitMicroShop();
// //更新用户是否是分销商
// $status1 = $microShopObj->updateMemberIsTarento();
// //给所有分销商开吃货
// if($status1){
//     $status2 = $microShopObj->initAllMicroShop();
// }
// //初始化合伙人层级
// if($status2){
//     $status3 = $microShopObj->initTarento();
// }
// //找出可享有99元开红人馆的用户并标记
// if($status3){
//     $status4 = $microShopObj->updateTarentoHaveDiscountPrice();
// }

// if($status4){
//     echo "完成！";
// }else{
//     echo "未成功<br />更新用户是否是分销商:".$status1."<br/>给所有分销商开吃货:".$status2."<br />初始化合伙人层级:".$status3."<br/>找出可享有99元开红人馆的用户并标记:".$status4;
// }
// exit();
/***********************/

/**
 * 初始化微店
 * @author 志杨
 *
 */
class InitMicroShop
{
    private $db;
    
    private $config;
    
    /**
     * 官方店member_id
     */
    private $officialMemberId = 14441;
    
    /**
     * 构造
     */
    public function __construct()
    {
        global $db,$config;
        $this->db = $db;
        $this->config = $config;
    }
    
    /**
     * 更新用户是否是分销商
     * @return boolean
     */
    public function updateMemberIsTarento(){
        $sql = "update ".MEMBER." a left join ".TARENTO." b on a.userid=b.member_id set a.is_tarento=2 where b.relations!=''";
        $this->db->query($sql);
        
        return true;
    }
    
    /**
     * 给所有分销商开吃货
     * @return boolean
     */
    public function initAllMicroShop(){
        //先开官方店
        $sql = "insert into ".MICROSHOP." (`member_id`,`level`,`status`,`name`) values ('{$this->officialMemberId}','1','1','洋玩易官方首席馆')";
        $this->db->query($sql);
        
        //查出需要开店的用户
        $sql = "select member_id from ".TARENTO." where relations!='' and member_id!={$this->officialMemberId}";
        $this->db->query($sql);
        $res = $this->db->getRows();
        if(!empty($res)){
            $values = "";
            $mids = "";
            foreach ($res as $v)
            {
                $values .= empty($values)?"('{$v['member_id']}','3','1','{$v['member_id']}的店铺')":",('{$v['member_id']}','3','1','{$v['member_id']}的店铺')";
                $mids .= empty($mids)?$v['member_id']:",".$v['member_id'];
            }
            
            if(!empty($values)){
                //写入微店表
                $sql = "insert into ".MICROSHOP." (`member_id`,`level`,`status`,`name`) values {$values}";
                $this->db->query($sql);
                
                //更新tarento表标记为已开店
                $sql = "update ".TARENTO." set have_micro_shop=1 where member_id in ({$mids})";
                $this->db->query($sql);
                
                return true;
            }
            
            return false;
        }
        
        return false;
    }
    
    /**
     * 初始化合伙人层级
     * @return boolean
     */
    public function initTarento(){
        //更新官方店下面的
        $sql = "update ".TARENTO." set `level`=`level`+1 where relations like '{$this->officialMemberId},_%'";
        $this->db->query($sql);
        
        //不在官方店下面的
        $sql = "update ".TARENTO." set relations=CONCAT('{$this->officialMemberId},',`relations`),`level`=`level`+2 where relations!='' and relations not like '{$this->officialMemberId},%'";
        $this->db->query($sql);
        
        
        //更新所有用户的直接和间接推荐人
        $sql = "select member_id,relations from ".TARENTO." where relations like '{$this->officialMemberId},_%'";
        $this->db->query($sql);
        $memList = $this->db->getRows();
        if(!empty($memList)){
            foreach ($memList as $val)
            {
                $directMidTemp = str_replace(','.$val['member_id'].',', '', $val['relations']);//去掉自己的部分
                $directMid = strpos($directMidTemp, ',')==false?$directMidTemp:strrchr($directMidTemp, ',');
                $directMid = trim($directMid,',');
                if($directMid!=$this->officialMemberId){
                    $indirectMidTemp1 = str_replace(','.$directMid, '', $directMidTemp);//去掉直接推荐人的部分
                    $indirectMid = strpos($indirectMidTemp1, ',')==false?$indirectMidTemp1:strrchr($indirectMidTemp1, ',');
                    $indirectMid = trim($indirectMid,',');
                }else{
                    $indirectMid = 0;
                }
                
                //更新
                $sql = "update ".TARENTO." set direct_rec_mid={$directMid},indirect_rec_mid={$indirectMid} where member_id={$val['member_id']}";
                $this->db->query($sql);
            }
        }
        
        return true;
    }
    
    /**
     * 找出可享有99元开红人馆的用户并标记
     * @return boolean
     */
    public function updateTarentoHaveDiscountPrice(){
        //查找分销商下单满1000的用户
        $sql = "SELECT
            	a.member_id,
            	IFNULL(SUM(
            		b.cost + b.freight + b.insurance_fee - b.discount - b.cpns_price
            	),0) as total_price
                FROM
                	".TARENTO." a
                LEFT JOIN ".ORDER." b ON a.member_id = b.member_id
                WHERE a.relations!=''
                GROUP BY a.member_id
                HAVING total_price>=1000";
        $this->db->query($sql);
        $list = $this->db->getRows();
        if(!empty($list)){
            $mids = '';
            foreach ($list as $val)
            {
                $mids .= empty($mids)?$val['member_id']:','.$val['member_id'];
            }
            
            $sql = "update ".TARENTO." set have_discount_price=1 where member_id in({$mids})";
            $this->db->query($sql);
            
            return true;
        }
        
        return false;
    }
}
?>